package com.christina.controller;

import com.christina.service.MessageQueueService;
import com.christina.utils.RedisUtil;
import com.christina.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 * 
 * @author Christina
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/test")
@Tag(name = "测试接口", description = "用于测试系统功能的接口")
public class TestController {

    private final RedisUtil redisUtil;
    private final MessageQueueService messageQueueService;

    public TestController(RedisUtil redisUtil, MessageQueueService messageQueueService) {
        this.redisUtil = redisUtil;
        this.messageQueueService = messageQueueService;
    }

    @GetMapping("/hello")
    @Operation(summary = "Hello测试", description = "简单的Hello World测试接口")
    public Result<String> hello() {
        log.info("Hello测试接口被调用");
        return Result.success("Hello, Christina Assistant!");
    }

    @GetMapping("/info")
    @Operation(summary = "系统信息", description = "获取系统基本信息")
    public Result<Map<String, Object>> info() {
        Map<String, Object> info = new HashMap<>();
        info.put("service", "christina-assistant");
        info.put("version", "1.0.0");
        info.put("timestamp", LocalDateTime.now());
        info.put("status", "running");
        
        log.info("系统信息接口被调用");
        return Result.success("获取系统信息成功", info);
    }

    @GetMapping("/swagger")
    @Operation(summary = "Swagger访问测试", description = "测试Swagger UI是否可以正常访问")
    public Result<Map<String, String>> swagger() {
        Map<String, String> links = new HashMap<>();
        links.put("swagger-ui", "/api/swagger-ui.html");
        links.put("api-docs", "/api/v3/api-docs");
        links.put("api-docs-yaml", "/api/v3/api-docs.yaml");
        
        log.info("Swagger访问测试接口被调用");
        return Result.success("Swagger访问链接", links);
    }

    /**
     * 测试Redis
     */
    @GetMapping("/redis")
    @Operation(summary = "Redis测试", description = "测试Redis连接和基本操作")
    public Result<String> testRedis() {
        try {
            String key = "test:redis:" + System.currentTimeMillis();
            String value = "Hello Redis!";
            
            // 设置值
            redisUtil.setString(key, value);
            
            // 获取值
            String result = redisUtil.getString(key);
            
            // 删除值
            redisUtil.del(key);
            
            log.info("Redis测试成功，key: {}, value: {}", key, result);
            return Result.success("Redis测试成功", result);
        } catch (Exception e) {
            log.error("Redis测试失败", e);
            return Result.error(500, "Redis测试失败: " + e.getMessage());
        }
    }

    /**
     * 测试RabbitMQ
     */
    @PostMapping("/rabbitmq")
    @Operation(summary = "RabbitMQ测试", description = "测试RabbitMQ消息发送")
    public Result<String> testRabbitMQ(@RequestParam String message) {
        try {
            Long testUserId = 999L;
            
            // 发送聊天消息
            messageQueueService.sendChatMessage(testUserId, message);
            
            // 发送任务消息
            messageQueueService.sendTaskMessage(1L, "TEST", "测试任务数据");
            
            // 发送通知消息
            messageQueueService.sendNotificationMessage(testUserId, "TEST", "测试通知内容");
            
            log.info("RabbitMQ测试消息发送成功");
            return Result.success("RabbitMQ测试成功，消息已发送到队列");
        } catch (Exception e) {
            log.error("RabbitMQ测试失败", e);
            return Result.error(500, "RabbitMQ测试失败: " + e.getMessage());
        }
    }
}